package proxy

import (
	"go-proxy/common"
	"go-proxy/config"
	"go-proxy/proxy/auth"
	"net/http"
)

type WebProxy struct {
	fileServer http.Handler
	server     *config.ProxyServer
}

func (web *WebProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// 健康检查
	if serverHealthCheck(r) {
		w.WriteHeader(http.StatusOK)
		return
	}

	common.AccessReqLogInfo(r, "web http request")

	// 添加前置监控指标
	HandleServerBeforeMetrics(web.server)
	AddUploadBytes(r.ContentLength)

	// 认证检查
	if !auth.HandleAuth(w, r, web.server.AuthName) {
		common.AccessReqLogInfo(r, "auth failed server=%s", web.server.Name)
		return
	}

	handleResponse(web.server, web.fileServer, w, r)
}
